package MyDisplayObjects 
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedClassName;
	
	/** An object in this class loads an image from the images folder and allows it to be dragged */
	public class DraggableImage extends DraggableMC
	{
		private var mc:MovieClip;
		private var oimageName:String;
		private var ldr:Loader;
		private var WIDTH:Number, HEIGHT:Number;
		private var constrainProportions:Boolean;
		
		
		override public function get width():Number{if (ldr != null){return ldr.width} else {return 0;}} 
		override public function get height():Number{if (ldr != null){return ldr.height} else {return 0;}} 
		override public function set width(w:Number):void{if (ldr != null){ldr.width=w} } 
		override public function set height(h:Number):void{if (ldr != null){ldr.height=h} } 
		
		//override public function set scaleX(w:Number):void{if (ldr != null){ldr.scaleX=w;} } 
		//override public function set scaleY(h:Number):void{if (ldr != null){ldr.scaleY=h} } 
		//override public function get mouseX():Number{return ldr.mouseX;}
		//override public function get mouseY():Number{return ldr.mouseY;}
		
		override public function play():void{if (mc != null) mc.play();}
		override public function stop():void{if (mc != null) mc.stop();}
		
		/** Constructor needs name of image file (ending with file type, e.g., .gif).*/
		public function DraggableImage(width:Number, height:Number, imageName:String, constrainProportions:Boolean = true, scaleOnMouseMultiplier:Number = 1)
		{
			super(scaleOnMouseMultiplier);
			this.WIDTH = width;
			this.HEIGHT = height;
			this.constrainProportions = constrainProportions; 
			oimageName = imageName;
			ldr = new Loader();
			var urlReq:URLRequest = new URLRequest(String("MathTextModeler/images/"+imageName));
			ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoadedImage);
			ldr.load(urlReq);
			
		}
		private function handleLoaderError (event:IOErrorEvent):void
		{
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoadedImage);
			this.dispatchEvent( new Event(Event.CANCEL));
		}
		/** Once the image has been loaded, display it */
		private function handleLoadedImage (event:Event):void
		{
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoadedImage);
			ldr.mouseEnabled = false;
			if (flash.utils.getQualifiedSuperclassName(ldr.content) == "flash.display::MovieClip")
			{
				mc = ldr.content as MovieClip;
				mc.stop();
			}
			ldr.x=0; ldr.y=0;
			if (constrainProportions)
			{
				//if (ldr.width/WIDTH > 1 || ldr.height/HEIGHT > 1)
				//{
					var prop:Number;
					// wider or taller
					if (ldr.width/WIDTH > ldr.height/HEIGHT)
					{
						prop = WIDTH/ldr.width;
					} else
					{
						prop = HEIGHT/ldr.height;
					}
					ldr.scaleX = prop;
					ldr.scaleY = prop;
				//}
			} else
			{
				ldr.scaleX = WIDTH/ldr.width;
				ldr.scaleY = HEIGHT/ldr.height;
			}
			
			this.addChild(ldr);		
			//this.graphics.clear();
			//this.graphics.beginFill(0x000000);
			//this.graphics.drawRect(0, 0, ldr.width, ldr.height);
			//this.graphics.endFill();
			this.dispatchEvent( new Event(Event.COMPLETE));
		}
		
		
		override public function duplicate():DraggableMC 
		{
			var dmc:DraggableImage = new DraggableImage(WIDTH, HEIGHT, this.oimageName, constrainProportions, scaleOnMouseMultiplier);
			dmc.x = this.x;
			dmc.y = this.y;
			dmc.name = this.name;
			dmc.type = this.type;
			return dmc;
		}
	}
}